import sys
import json
from forms.uipy.UI_report_review_收支 import Ui_report_review
from PySide2.QtWidgets import QWidget, QApplication, QLabel, QGridLayout, QLineEdit, QPushButton, QMessageBox
from tools.File_Select_Tools import File_Select_Tools
from server.ReportReviewServer_收支 import ReportReviewServer


# 收支年报审核实现类
class ReportReviewImpl_收支(QWidget, Ui_report_review):
    def __init__(self):
        super().__init__()
        # 获取配置json文件 数据
        # with open(r"./res/审核部分配置文件/年报配置.json") as f:

        self.pre_nb_path = None
        with open(r"E:\泸州调查队居民收支科\居民收支科_win7\res\审核部分配置文件\年报配置.json", encoding="UTF-8") as f:
            self.pz_data: dict = json.load(f)
        self.setupUi(self)



        # 设置 报表选择 数据项
        self.Report_CB.clear()
        self.Report_CB.addItems(self.pz_data.keys())

        self.Report_CB.currentTextChanged.connect(self.Report_CB_changed_handle)

        self.fun_show_layout = QGridLayout()
        self.fun_show_layout.setMargin(0)

        layout = self.fun_show_layout
        layout.addWidget(QLabel("单季报表"), 0, 0, 1, 1)
        self.jb_path = QLineEdit()
        layout.addWidget(self.jb_path, 0, 1, 1, 1)
        btn = QPushButton("选择")
        btn.clicked.connect(
            lambda: self.jb_path.setText(File_Select_Tools.select_one_file(title="单季报表选择")[0]))
        layout.addWidget(btn, 0, 2, 1, 1)

        self.fun_show.setLayout(self.fun_show_layout)
        # # self.fun_show.setlayout(self.fun_show_layout)

        self.out_path_btn.clicked.connect(
            lambda: self.out_path.setText(File_Select_Tools.select_dir(title="输出文件夹选择")))

        self.ok_run.clicked.connect(self.ok_run_handler)

        # 根据用户不同选择，生成对应额功能
        # print(self.pz_data.keys())

    def ok_run_handler(self):
        currentText = self.Report_CB.currentText()
        reg_txt = self.Report_reg.currentText()

        if currentText == "单季报表审核":
            #  单季报表审核
            # if reg_txt == "全部":
            #     # 执行全部

            pass
        elif currentText == "两季报表审核":
            #  两季报表审核

            pass
        elif currentText == "单年报表审核":
            #  单年报表审核
            rrs = ReportReviewServer(self.pz_data, self.out_path.text(), None, self.nb_path.text())
            if reg_txt == "全部":
                rrs.经营成本一二三产存疑_总收入_未扣除生产费用()
                rrs.经营成本一二三产存疑_现金收入_未扣除生产费用()

            elif reg_txt == "经营成本一二三产存疑_总收入(未扣除生产费用)":
                rrs.经营成本一二三产存疑_总收入_未扣除生产费用()
            elif reg_txt == "经营成本一二三产存疑_现金收入(未扣除生产费用)":
                rrs.经营成本一二三产存疑_现金收入_未扣除生产费用()

            pass
        elif currentText == "两年报表审核":
            #  两年报表审核
            rrs = ReportReviewServer(self.pz_data, self.out_path.text(), self.pre_nb_path.text(), self.now_nb_path.text())
            if reg_txt == "全部":
                rrs.调味品变化存疑()
                rrs.常住人口变动存疑()
                rrs.养老金变动存疑()
                rrs.食品消费存疑()
            elif reg_txt == "调味品变化存疑":
                rrs.调味品变化存疑()
            elif reg_txt == "常住人口变动存疑":
                rrs.常住人口变动存疑()
            elif reg_txt == "养老金变动存疑":
                rrs.养老金变动存疑()
            elif reg_txt == "食品消费存疑":
                rrs.食品消费存疑()

        QMessageBox.information(self, '提示', '执行完成')
        pass

    def Report_CB_changed_handle(self, current_text):
        """
        报表选择下拉框 改变调用函数
        :param current_text: 当前选择的值
        :return: None
        """

        self.clear_widget(self.fun_show)
        layout = self.fun_show_layout

        # 根据选择的不同添加对应的功能部分
        if current_text == "单季报表审核":
            layout.addWidget(QLabel("单季报表"), 0, 0, 1, 1)
            self.jb_path = QLineEdit()
            layout.addWidget(self.jb_path, 0, 1, 1, 1)
            btn = QPushButton("选择")
            btn.clicked.connect(
                lambda: self.jb_path.setText(File_Select_Tools.select_one_file(title="单季报表选择")[0]))
            layout.addWidget(btn, 0, 2, 1, 1)

        elif current_text == "两季报表审核":

            layout.addWidget(QLabel("前季报表"), 0, 0, 1, 1)
            self.pre_jb_path = QLineEdit()
            layout.addWidget(self.pre_jb_path, 0, 1, 1, 1)
            btn1 = QPushButton("选择")
            btn1.clicked.connect(
                lambda: self.pre_jb_path.setText(File_Select_Tools.select_one_file(title="前季报表选择")[0]))
            layout.addWidget(btn1, 0, 2, 1, 1)

            layout.addWidget(QLabel("后季报表"), 1, 0, 1, 1)
            self.now_jb_path = QLineEdit()
            layout.addWidget(self.now_jb_path, 1, 1, 1, 1)
            btn2 = QPushButton("选择")
            btn2.clicked.connect(
                lambda: self.now_jb_path.setText(File_Select_Tools.select_one_file(title="后季报表选择")[0]))
            layout.addWidget(btn2, 1, 2, 1, 1)

        elif current_text == "单年报表审核":
            layout.addWidget(QLabel("单年报表"), 0, 0, 1, 1)
            self.nb_path = QLineEdit()
            layout.addWidget(self.nb_path, 0, 1, 1, 1)
            btn = QPushButton("选择")
            btn.clicked.connect(lambda: self.nb_path.setText(File_Select_Tools.select_one_file(title="单年报选择")[0]))
            layout.addWidget(btn, 0, 2, 1, 1)

        elif current_text == "两年报表审核":

            layout.addWidget(QLabel("前年报表"), 0, 0, 1, 1)
            self.pre_nb_path = QLineEdit()
            layout.addWidget(self.pre_nb_path, 0, 1, 1, 1)
            btn1 = QPushButton("选择")
            btn1.clicked.connect(
                lambda: self.pre_nb_path.setText(File_Select_Tools.select_one_file(title="前年报表选择")[0]))
            layout.addWidget(btn1, 0, 2, 1, 1)

            layout.addWidget(QLabel("后年报表"), 1, 0, 1, 1)
            self.now_nb_path = QLineEdit()
            layout.addWidget(self.now_nb_path, 1, 1, 1, 1)
            btn2 = QPushButton("选择")
            btn2.clicked.connect(
                lambda: self.now_nb_path.setText(File_Select_Tools.select_one_file(title="后年报表选择")[0]))
            layout.addWidget(btn2, 1, 2, 1, 1)

        # 设置 审核公式值
        regs = self.pz_data.get(current_text).keys()
        self.Report_reg.clear()
        if len(regs) == 0:
            self.Report_reg.setEnabled(False)
        else:
            self.Report_reg.setEnabled(True)
            self.Report_reg.addItem("全部")
            self.Report_reg.addItems(self.pz_data.get(current_text).keys())

    # 删除所有控件
    def clear_widget(self, widget):
        layout = widget.layout()
        if layout:
            while layout.count():
                item = layout.takeAt(0)
                child_widget = item.widget()
                if child_widget:
                    child_widget.setParent(None)
                    child_widget.deleteLater()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = ReportReviewImpl_收支()
    ui.show()
    sys.exit(app.exec_())
